package com.itoffer.service.impl;

import com.itoffer.mapper.CompanyMapper;
import com.itoffer.pojo.*;
import com.itoffer.service.CompanyService;
import com.itoffer.util.SqlSessionFactoryUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import java.util.List;

public class CompanyServiceImpl implements CompanyService {
    SqlSessionFactory factory = SqlSessionFactoryUtils.getSqlSessionFactory();

    @Override
    public Company selectByCompanyId(int company_id) {
        SqlSession sqlSession = factory.openSession();
        CompanyMapper mapper = sqlSession.getMapper(CompanyMapper.class);
        Company company = mapper.selectCompanyInformation(company_id);
        sqlSession.close();
        return company;
    }

    @Override
    public Job selectJobByCompanyId(int company_id) {
        SqlSession sqlSession = factory.openSession();
        CompanyMapper mapper = sqlSession.getMapper(CompanyMapper.class);
        Job job = mapper.selectJobInformationByCompanyId(company_id);
        sqlSession.close();
        return job;
    }

    @Override
    public boolean addJobApply(int job_id, int applicant_id, String apply_date) {
        SqlSession sqlSession = factory.openSession();
        CompanyMapper mapper = sqlSession.getMapper(CompanyMapper.class);
        int i = mapper.addJobApply(job_id, applicant_id, apply_date);
        sqlSession.commit();
        sqlSession.close();
        return i > 0;
    }

    @Override
    public CompanyJobName selectCompanyAndJobNameByJObId(int job_id) {
        SqlSession sqlSession = factory.openSession();
        CompanyMapper mapper = sqlSession.getMapper(CompanyMapper.class);
        CompanyJobName companyJobName = mapper.selectJobInfoByJobId(job_id);
        sqlSession.close();
        return companyJobName;
    }

    @Override
    public List<JobApply> selectJobApplyByApplicantId(int applicant_id) {
        SqlSession sqlSession = factory.openSession();
        CompanyMapper mapper = sqlSession.getMapper(CompanyMapper.class);
        List<JobApply> jobApplies = mapper.selectJobApplyByApplicantId(applicant_id);
        sqlSession.close();
        return jobApplies;
    }

    @Override
    public boolean deleteApplyByApplyId(int apply_id) {
        SqlSession sqlSession = factory.openSession();

        CompanyMapper mapper = sqlSession.getMapper(CompanyMapper.class);
        int i = mapper.deleteJobApplyByApplyId(apply_id);
        sqlSession.commit();
        sqlSession.close();
        return i > 0;
    }

    @Override
    public void deleteByApplyIds(int[] ids) {
        SqlSession sqlSession = factory.openSession();
        CompanyMapper mapper = sqlSession.getMapper(CompanyMapper.class);
        mapper.deleteByApplyIdS(ids);
        sqlSession.commit();
        sqlSession.close();
    }

    @Override
    public PageBean<JobApply> selectByPage(int applicant_id, int current, int pageSize) {
        SqlSession sqlSession = factory.openSession();
        CompanyMapper mapper = sqlSession.getMapper(CompanyMapper.class);
//  计算开始索引
        int begin = (current - 1) * pageSize;
//计算查询条目数
        int size = pageSize;
//  查询当前页数据
        List<JobApply> rows = mapper.selectByPage(applicant_id, begin, size);
        //查询总记录数
        int total = mapper.totalSize(applicant_id);
        //封装PageBean对象
        PageBean<JobApply> PageBean = new PageBean<>();
        PageBean.setRows(rows);
        PageBean.setTotalCount(total);
        sqlSession.close();
        return PageBean;
    }
}
